package com.estate.struts.actions;


import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.estate.business.AllProjectService;
import com.estate.business.BillingService;
import com.estate.business.PriceProjectService;
import com.estate.business.UserInfoService;
import com.estate.utils.ProCatagoryUtil;

import com.estate.vo.impl.BillingVo;

import com.estate.vo.impl.UserInfoVo;

public class ChargeAction extends EstateSupport {

	
	private int id;
	private String cashNum;          
	private BillingVo billing;       
	private String proName;			
	private int proCatagory;		
	private int proId;
	private String cashMoney;
	private UserInfoVo cashUser;
	private Date cashDate;
	private int lastRecordUserId;
	private int cashUserId;
	
	private BillingService billingService;
	private PriceProjectService priceProjectService;
	private UserInfoService userInfoService;
	private AllProjectService allProjectService;
	
	private String multiType;
	private String multiDelete;
	private String delStr;
	private String deleteId;
	private int modifyId;
	
	private String method;


	public String execute() throws Exception {
		System.out.println("in billing");
		String page = "";

		if ("list".equals(method)) {

			List<BillingVo> voList = billingService.findAllBilling();
			voList.removeAll(billingService.findBillByState(1));
			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			for(int i=0;i<voList.size();i++){
				if(voList.get(i).getState()==2)
					voList.get(i).setStateName("未收款");
				else voList.get(i).setStateName("已收款");
			}
			this.getSession().setAttribute("charge", voList);
			page = "list";

		} else if ("delete".equals(method)) {
			if ("true".equals(multiDelete)) {
				System.out.println("!!!!!!!!!" + delStr);
				if (delStr != null && !"".equals(delStr) && !",".equals(delStr)) {
					String[] delArray = delStr.split(",");
					for (int i = 0; i < delArray.length; i++) {
						billingService
								.deleteByPK(Integer.parseInt(delArray[i]));
					}
				}
			} else {
				billingService.deleteByPK(Integer.parseInt(deleteId));
			}
			page = "refresh";
		}   else if("modify".equals(method)){
			BillingVo modifyList=billingService.findBillingByPK(modifyId);
			HashMap<String, Integer> map = userInfoService.findAllNameAndId();
			ServletActionContext.getRequest().setAttribute("chargeUserSelect",
					map);
			modifyList.setProCatagoryName(ProCatagoryUtil.getProCatagoryString(modifyList.getProCatagory()));
			this.getSession().setAttribute("modifyList", modifyList);
			this.getSession().setAttribute("billUser", modifyList.getBillUser());
			page="modify";
		} else if("update".equals(method)){
			BillingVo vo=billingService.findBillingByPK(modifyId);
			vo=getModifyInfo(vo);
			billingService.update(vo);
			page="refresh";
		}
		return page;
	}
	
	public BillingVo getModifyInfo(BillingVo vo){
		vo.setCashDate(cashDate);
		vo.setCashMoney(cashMoney);
		vo.setCashNum(cashNum);
		vo.setCashUser((UserInfoVo)userInfoService.getUserInfo(cashUserId));
		vo.setLastRecordUserId(this.getSessionUser().getId());
		vo.setState(3);
		return vo;
	}



	public void setBillingService(BillingService billingService) {
		this.billingService = billingService;
	}

	public BillingService getBillingService() {
		return billingService;
	}

	public void setPriceProjectService(PriceProjectService priceProjectService) {
		this.priceProjectService = priceProjectService;
	}

	public PriceProjectService getPriceProjectService() {
		return priceProjectService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setAllProjectService(AllProjectService allProjectService) {
		this.allProjectService = allProjectService;
	}

	public AllProjectService getAllProjectService() {
		return allProjectService;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setModifyId(int modifyId) {
		this.modifyId = modifyId;
	}

	public int getModifyId() {
		return modifyId;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setCashNum(String cashNum) {
		this.cashNum = cashNum;
	}

	public String getCashNum() {
		return cashNum;
	}

	public void setBilling(BillingVo billing) {
		this.billing = billing;
	}

	public BillingVo getBilling() {
		return billing;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getProName() {
		return proName;
	}

	public void setProCatagory(int proCatagory) {
		this.proCatagory = proCatagory;
	}

	public int getProCatagory() {
		return proCatagory;
	}

	public void setProId(int proId) {
		this.proId = proId;
	}

	public int getProId() {
		return proId;
	}

	public void setCashMoney(String cashMoney) {
		this.cashMoney = cashMoney;
	}

	public String getCashMoney() {
		return cashMoney;
	}

	public void setCashUser(UserInfoVo cashUser) {
		this.cashUser = cashUser;
	}

	public UserInfoVo getCashUser() {
		return cashUser;
	}

	public void setCashDate(Date cashDate) {
		this.cashDate = cashDate;
	}

	public Date getCashDate() {
		return cashDate;
	}

	public void setLastRecordUserId(int lastRecordUserId) {
		this.lastRecordUserId = lastRecordUserId;
	}

	public int getLastRecordUserId() {
		return lastRecordUserId;
	}

	public void setCashUserId(int cashUserId) {
		this.cashUserId = cashUserId;
	}

	public int getCashUserId() {
		return cashUserId;
	}
}
